package com.yc.myspringboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.yc.myspringboot.config.Convert;
import com.yc.myspringboot.ctr.TestController;
import com.yc.myspringboot.pojo.Person;

@Service
public class TestCacheService {
	
	@Autowired
	private TestController testController;
	@Autowired
	private ApplicationContext context;

	/**
	 * key=yc::aa
	 * @param id
	 * @return
	 */
	@Cacheable(cacheNames = Convert.CACHENAME_1, key="#{id}")
	public String m1(String id) {
		System.out.println("-TestCacheService----m1-------");
		return System.currentTimeMillis() + "";
	}
	
	/**
	 * key=yc::zhangsan-abcde
	 * @param name
	 * @param password
	 * @return
	 */
	@Cacheable(value=Convert.CACHENAME_1, key="T(String).valueOf(#name).concat('-').concat(#password)")
	public String m2(String name, String password) {
		System.out.println("-TestCacheService----m2-------");
		return System.currentTimeMillis() + "";
	}
	
	/**
	 * key=yc::abcde-zhangsan-12
	 * @param password
	 * @param person
	 * @return
	 */
	@Cacheable(value=Convert.CACHENAME_1, key="T(String).valueOf(#password).concat('-').concat(#person.name).concat('-').concat(#person.age)")
	public String m3(String password, Person person) {
		System.out.println("-TestCacheService----m3-------");
		return System.currentTimeMillis() + "";
	}
	
//	----------------------------------更简单的spEL拼接字符串方式----------------------------
	/**
	 * key=yc::aa_123
	 * @param id
	 * @return
	 */
	@Cacheable(cacheNames = Convert.CACHENAME_1, key="#id + '_' + 123")
	public String m4(String id) {
		System.out.println("-TestCacheService----m4-------");
		return System.currentTimeMillis() + "";
	}
	
	/**
	 * yc::abcde_zhangsan_12
	 * @param password
	 * @param person
	 * @return
	 */
	@Cacheable(cacheNames = Convert.CACHENAME_1, key="#password + '_' + #person.name + '_' + #person.age")
	public String m5(String password, Person person) {
		System.out.println("-TestCacheService----m5-------");
		return System.currentTimeMillis() + "";
	}
	
	/**
	 * 本方法清除缓存
	 * @param password
	 * @param person
	 * @return
	 */
	@Cacheable(cacheNames = Convert.CACHENAME_1, key="#password + '_' + #person.name + '_' + #person.age")
	public String m6(String password, Person person) {
		System.out.println("-TestCacheService----m6-------");
		new Thread(() -> {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			//清缓存
			TestCacheService tcs = context.getBean(TestCacheService.class);
			tcs.m7(password, person);
		}).start();
		return System.currentTimeMillis() + "";
	}
	
	@CacheEvict(cacheNames = Convert.CACHENAME_1, key="#password + '_' + #person.name + '_' + #person.age")
	public String m7(String password, Person person) {
		System.out.println("-TestCacheService----m7-------");
		return System.currentTimeMillis() + "";
	}
}
